package com.wy.util;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * 插件工具类
 *
 * @author 飞花梦影
 * @date 2021-07-19 09:20:26
 * @git {@link https://github.com/dreamFlyingFlower }
 */
public class PluginUtils {

	public static String SPRING_BOOT_GROUP_ID = "org.springframework.boot";

	public static String SPRING_CLOUD_GROUP_ID = "org.springframework.cloud";

	private static GridData GRID_DATA_TEXT = new GridData(GridData.FILL_HORIZONTAL);

	public static Text createText(Composite composite, ModifyListener modifyListener) {
		Text text = new Text(composite, SWT.BORDER | SWT.SINGLE);
		text.setLayoutData(GRID_DATA_TEXT);
		if (null != modifyListener) {
			text.addModifyListener(modifyListener);
		}
		return text;
	}

	public static Text createText(Composite composite, String labelText, ModifyListener modifyListener) {
		Label label = new Label(composite, SWT.NULL);
		label.setText(labelText);
		return createText(composite, modifyListener);
	}

	public static IJavaProject getIJavaProject(String containerName) {
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IResource resource = root.findMember(new Path(containerName));
		if (resource.exists() && (resource instanceof IContainer)) {
			IContainer container = (IContainer) resource;
			try {
				IJavaProject project =
						(IJavaProject) container.getProject().getNature("org.eclipse.jdt.core.javanature");
				return project;
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static String createFile(String containerName, String packageName, String fileName, IProgressMonitor monitor,
			String content) {
		monitor.beginTask("Creating " + fileName, 2);
		try {
			IJavaProject project = getIJavaProject(containerName);
			if (project == null) {
				return "Container \"" + containerName + "\" does not exist.";
			}
			IPackageFragment ipackage =
					project.getAllPackageFragmentRoots()[0].createPackageFragment(packageName, false, monitor);
			ipackage.createCompilationUnit(fileName + ".java", content, false, monitor);
		} catch (CoreException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void addPom(String containerName, String groupId, String artifactId, String version) {
		Model model = null;
		MavenXpp3Reader mavenreader = new MavenXpp3Reader();
		MavenXpp3Writer mavenwriter = new MavenXpp3Writer();
		IJavaProject iJavaProject = getIJavaProject(containerName);
		if (null == iJavaProject) {
			System.out.println(containerName + " 不存在");
			return;
		}
		FileWriter writer = null;
		try (FileReader reader =
				new FileReader(iJavaProject.getProject().getLocation() + File.separator + "pom.xml");) {
			model = mavenreader.read(reader, true);
			List<Dependency> dependencies = model.getDependencies();
			if (dependencies.isEmpty()) {
				writer = new FileWriter(iJavaProject.getProject().getLocation() + File.separator + "pom.xml");
				Dependency dependency = buildDependency(groupId, artifactId, version);
				dependencies.add(dependency);
				mavenwriter.write(writer, model);
			} else {
				boolean flag = true;
				for (Dependency dependency : dependencies) {
					if (artifactId.equals(dependency.getArtifactId())) {
						flag = false;
						break;
					}
				}
				if (flag) {
					writer = new FileWriter(iJavaProject.getProject().getLocation() + File.separator + "pom.xml");
					Dependency dependency = buildDependency(groupId, artifactId, version);
					dependencies.add(dependency);
					mavenwriter.write(writer, model);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (null != writer) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void addPomSpringBootWeb(String containerName) {
		addPom(containerName, SPRING_BOOT_GROUP_ID, "spring-boot-starter-web", null);
	}

	public static Dependency buildSpringBootWeb() {
		return buildDependency(SPRING_BOOT_GROUP_ID, "spring-boot-starter-web", null);
	}

	public static Dependency buildDependency(String groupId, String artifactId) {
		return buildDependency(groupId, artifactId, null);
	}

	public static Dependency buildDependency(String groupId, String artifactId, String version) {
		Dependency dependency = new Dependency();
		dependency.setGroupId(groupId);
		dependency.setArtifactId(artifactId);
		if (null != version && !version.isEmpty()) {
			dependency.setVersion(version);
		}
		return dependency;
	}
}